package cn.edu.scau.cmi.hexiaoqing.client;


import java.util.Set;

import cn.edu.scau.cmi.hexiaoqing.interProgramming.Multiton;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class MultitonClient extends Application {

	Set<Multiton> instances;

	@Override
	public void start(Stage primaryStage) {
		Text[] ts = new Text[5];
		for (int i = 0; i < 5; i++) {
			Text t = new Text();
			t.setFont(new Font(20));
			ts[i] = t;
		}
		BorderPane bp = new BorderPane();
		Button b1 = new Button("New a teacher");
		b1.setOnAction(e -> {
			stage2();
		});
		Button b2 = new Button("Get the teachers' name");
		VBox vb2 = new VBox(20);
		b2.setOnAction(e -> {
			instances = Multiton.getInstances();
			if (instances.size() == 0) {
				ts[0].setText("Please new a teacher!");
				bp.setCenter(ts[0]);
			} else {
				vb2.getChildren().clear();
				int i = 0;
				for (Multiton instance : instances) {
					ts[i].setText(instance.getName());
					vb2.getChildren().add(ts[i]);
					i++;
				}
				bp.setCenter(vb2);
			}
		});
		VBox vb = new VBox(20);
		vb.getChildren().add(b1);
		vb.getChildren().add(b2);
		bp.setPadding(new Insets(50, 10, 50, 50));
		bp.setRight(vb);
		Scene s = new Scene(bp, 500, 300);
		Stage stage = new Stage();
		stage.setTitle("Multiton");
		stage.setScene(s);
		stage.show();
	}

	public void stage2() {
		Stage stage = new Stage();
		VBox vb = new VBox(20);
		vb.setAlignment(Pos.CENTER);
		TextField tf = new TextField();
		Button b = new Button("Submit");
		b.setOnAction(e -> {
			String name = tf.getText();
			if (name.equals("")) {
				Stage stage1 = new Stage();
				Text t = new Text("Please enter a name!");
				t.setFont(new Font(20));
				BorderPane bp1 = new BorderPane();
				bp1.setCenter(t);
				Scene s1 = new Scene(bp1, 300, 200);
				stage1.setTitle("ERROR");
				stage1.setScene(s1);
				stage1.show();
			} else {
				instances = Multiton.getInstances();
				if (instances.size() == 5) {
					Stage stage1 = new Stage();
					Text t = new Text("The prominent teacher is full!");
					t.setFont(new Font(20));
					BorderPane bp1 = new BorderPane();
					bp1.setCenter(t);
					Scene s1 = new Scene(bp1, 300, 200);
					stage1.setTitle("ERROR");
					stage1.setScene(s1);
					stage1.show();
				} else {
					instances = Multiton.addInstances(name);
				}
				stage.close();
			}
		});
		vb.getChildren().add(tf);
		vb.getChildren().add(b);
		BorderPane bp = new BorderPane();
		bp.setCenter(vb);
		Scene s = new Scene(bp, 300, 200);
		stage.setTitle("New a teacher");
		stage.setScene(s);
		stage.show();

	}

	public static void main(String[] args) {
		Application.launch(args);
	}
}
